package cn.iocoder.yudao.common.util;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;


/**
 * @author dsc
 * @version 1.0
 */

@NoArgsConstructor
@Data
@Slf4j
public class LogPattern {


    private static final String COLON = ":";
    private static final String COMMA = ",";
    private static String messageKey = "message";
    private static String parameterKey = "params";
    private static String tip = "---";
    private static boolean alwaysShowParameter = true;

    private static final String LEFT_MIDDLE_BRACKET = "[";
    private static final String RIGHT_MIDDLE_BRACKET = "]";


    /**
     * 格式化日志，下面是代码示例
     * <p/>
     * {@code log.error(LogPattern.formalize("请求参数缺失","errMsg"),ex.getMessage(),ex);}
     */
    public static String formalize(String title, String... values) {
        try {
            boolean hasArgs = values != null && values.length > 0;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(tip).append(":").append(messageKey);
            if (title != null && !title.isEmpty()) {
                stringBuilder.append("[").append(title).append("]");
            }

            if (hasArgs) {
                stringBuilder.append(",").append(parameterKey).append(":[");
                int len = values.length;
                for (int i = 0; i < len; ++i) {
                    stringBuilder.append(values[i]).append("={}").append(i < len - 1 ? "," : "");
                }
                stringBuilder.append("]");
            }

            return stringBuilder.toString();
        } catch (Exception e) {
            log.error("格式化日志出现问题 msg：{}", e.getMessage(), e);
            return title;
        }
    }

}
